package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.Clotheshorse;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 晾衣架数据库操作
 */

public class ClotheshorseDao {
    private Context mContext;
    private Dao<Clotheshorse, Integer> clotheshorseDaoOpe;
    private DatabaseHelper helper;

    public ClotheshorseDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            clotheshorseDaoOpe = helper.getDao(Clotheshorse.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(Clotheshorse clotheshorse) {
        try {
            clotheshorse.setChecked(false);
            clotheshorseDaoOpe.create(clotheshorse);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateClotheshorse(Clotheshorse clotheshorse) {
        try {
            clotheshorse.setChecked(false);
            return clotheshorseDaoOpe.update(clotheshorse);
            //    LogHelper.i("updateLightClotheshorsenumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public Clotheshorse get(int id) {
        try {
            return clotheshorseDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public Clotheshorse getByUuid(String uuid) {
        List<Clotheshorse> curtainList = null;
        try {
            curtainList = clotheshorseDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (curtainList != null && curtainList.size() > 0) {
                return curtainList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * @return
     */
    public List<Clotheshorse> getAllClotheshorse() {
        List<Clotheshorse> clotheshorseList = new ArrayList<>();
        try {
            clotheshorseList = clotheshorseDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return clotheshorseList;
    }

    public Clotheshorse getById(String clotheshorseId,int category) {
        List<Clotheshorse> clotheshorseList = null;
        try {
            clotheshorseList = clotheshorseDaoOpe.queryBuilder().where().eq
                    ("clotheshorseId", clotheshorseId).and().eq
                    ("category", category).query();
            if (clotheshorseList != null && clotheshorseList.size() > 0) {
                return clotheshorseList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<Clotheshorse> getFamilyClotheshorses(String familyId,int category) {
        List<Clotheshorse> clotheshorseList = new ArrayList<>();
        try {
            clotheshorseList = clotheshorseDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return clotheshorseList;
    }

    public int delete(Clotheshorse clotheshorse) {
        try {
            return clotheshorseDaoOpe.delete(clotheshorse);
            //    LogHelper.i("deleLightClotheshorsenumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = clotheshorseDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<Clotheshorse> clotheshorseList) {
        try {
            int number = clotheshorseDaoOpe.delete(clotheshorseList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyClotheshorses(String familyId) {
        try {
            List<Clotheshorse> clotheshorseList = getFamilyClotheshorses(familyId,7);
            int number = clotheshorseDaoOpe.delete(clotheshorseList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Clotheshorse getClotheshorseByName(String familyId, String name,int category) {
        try {
            List<Clotheshorse> clotheshorseList = clotheshorseDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (clotheshorseList != null && clotheshorseList.size() > 0)
                return clotheshorseList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
